/**
 * 
 */
package componentEnhancement;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.configuration.XMLConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;

import cii_lib.CCII_ACCESS_INTERF;
import cii_lib.CCII_ACCESS_PARAM;
import cii_lib.CCII_COMPONENT;
import cii_lib.CCII_TABLE_INDEX;

/**
 * @author pdrabik
 *
 */
public class ContextUtils {
	
	private static final Logger log = LoggerFactory.getLogger(ContextUtils.class);

	 private Vector<CCII_ACCESS_INTERF> interfaces = new Vector<CCII_ACCESS_INTERF>();
	 private Vector<CCII_ACCESS_PARAM> params = new Vector<CCII_ACCESS_PARAM>();
	 private Vector<CCII_COMPONENT> children = new Vector<CCII_COMPONENT>();
	 private CCII_COMPONENT komp = null;
	 
	 private HashMap<String, CCII_ACCESS_INTERF> mapInterf = new HashMap<String, CCII_ACCESS_INTERF>();
	 protected HashMap<String, CCII_ACCESS_PARAM> mapParam = new HashMap<String, CCII_ACCESS_PARAM>();
	 protected HashMap<String, CCII_COMPONENT> mapChildren = new HashMap<String, CCII_COMPONENT>();
	 
	 public ContextUtils() {}
	 
	 public ContextUtils(CCII_COMPONENT comp) {
		 Vector<CCII_ACCESS_INTERF> argI = comp.getInterfaces();
		 Vector<CCII_ACCESS_PARAM> argP = comp.getParams();
		 children = comp.getChildren();
		 komp=comp;
		 interfaces = argI;
		 params = argP;
		 for (int i=0; i< argI.size();i++) {
			 CCII_ACCESS_INTERF tmp = argI.get(i);
			 mapInterf.put(tmp.Name(), tmp);
		 }
		 for (int i=0; i< argP.size();i++) {
			 CCII_ACCESS_PARAM tmp = argP.get(i);
			 mapParam.put(tmp.Name(), tmp);
		 }
		 for (int i=0; i< children.size();i++) {
			 CCII_COMPONENT tmp = children.get(i);
			 mapChildren.put(tmp.Name(), tmp);
		 }
	 }
	 
	 public ContextUtils(Vector<CCII_ACCESS_INTERF> argI, Vector<CCII_ACCESS_PARAM> argP) {
		 interfaces = argI;
		 params = argP;
		 for (int i=0; i< argI.size();i++) {
			 CCII_ACCESS_INTERF tmp = argI.get(i);
			 mapInterf.put(tmp.Name(), tmp);
		 }
		 for (int i=0; i< argP.size();i++) {
			 CCII_ACCESS_PARAM tmp = argP.get(i);
			 mapParam.put(tmp.Name(), tmp);
		 }

	 }
	 public CCII_ACCESS_PARAM getParamByName(String key) {
		 if (mapParam.size()>0) return mapParam.get(key);
		 else {
			 for (int i=0; i<params.size(); i++) {
				 CCII_ACCESS_PARAM tmp =params.get(i);
				 if (tmp.GetItem().Name==key) return tmp;
			 }
			 return null;
		 }
	 }
	 
	 public static CCII_ACCESS_INTERF getInterfByName(CCII_COMPONENT comp,String key) throws Exception{
		 return (new ContextUtils(comp)).getInterfByName(key);
	 }
	 
	 public CCII_ACCESS_INTERF getInterfByName(String key) throws Exception{
		 if (mapInterf.size()>0) return mapInterf.get(key);
		 else {
			 for (int i=0; i<interfaces.size(); i++) {
				 CCII_ACCESS_INTERF tmp =interfaces.get(i);
				 log.debug("reg--"+tmp.GetItem().Name);
				 if (tmp.GetItem().Name==key) return tmp;
			 }
			 throw new Exception("ContextUtils\\getInterfByName: cant find interface with:" 
					 +key.toUpperCase()+" componenet: " +komp.Name());
//			 return null;
		 }
	 }
	 
	 public static CCII_COMPONENT getChildByName(CCII_COMPONENT comp,String key) {
		 return (new ContextUtils(comp)).getChildByName(key);
	 }
	 
	 public CCII_COMPONENT getChildByName(String key) {
		 if (mapChildren.size()>0) return mapChildren.get(key);
		 else {
			 for (int i=0; i<children.size(); i++) {
				 CCII_COMPONENT tmp =mapChildren.get(i);
				 if (tmp.GetItem().Name==key) return tmp;
			 }
			 return null;
		 }
	 }
	 
//	 public XMLConfiguration getConfigPad(CCII_COMPONENT comp) {
//		 
//	 }
	 
	 public static XMLConfiguration getConfigFromCU(final ContextUtils cu) {
		 return getConfigFromPathName(cu.getComponent());
	 }
	 
	 public static XMLConfiguration getConfigFromPathName(final CCII_TABLE_INDEX cip) {
		 	String path="";
		 	String configLocation="ciiconfig";
		 	XMLConfiguration config = new XMLConfiguration() ;
		 	if (cip instanceof CCII_ACCESS_INTERF) {
		 		path=((CCII_ACCESS_INTERF) cip).PathName();
		 	} else 
		 	if (cip instanceof CCII_COMPONENT) {
		 		path=((CCII_COMPONENT) cip).PathName();
		 	} else 
		 	if (cip instanceof CCII_ACCESS_PARAM) {
		 		path=((CCII_ACCESS_PARAM) cip).PathName();
		 	}
		 	path=path.replaceAll("[0-9\\[\\]]", "");
		 try {
		 	config.load(new File(configLocation+"/system.xml"));
		 	String compKey = "";
		 	for (String a : path.split("\\.")) {
		 		
//		 	log.debug("parts: " +a + " found: "+ config.containsKey(a));
		 	if (config.containsKey(a)) compKey=a;
		 	}
		 	String compPath =config.getString(compKey);
		 	return new XMLConfiguration(new File(configLocation+"/"+compPath));
		 } catch (Exception e) {
			log.error("", e);
		}
		 return null;
	 }
	 
	 public HashMap<String, CCII_ACCESS_INTERF> getInterfaceMap() {
		 return mapInterf;
	 }
	 public HashMap<String, CCII_ACCESS_PARAM> getParamsMap() {
		 return mapParam;
	 }
	 /**
		 * @return the komp
		 */
		public CCII_COMPONENT getComponent() {
			return komp;
		}
	 
}
